package com.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import com.module.commonui.R;
import com.tange.base.toolkit.StringUtils;

public class SettingItemView extends RelativeLayout {
    private ImageView icon;
    private boolean check;
    private float itemTextSize;
    private float markTextSize;
    private String mName;
    private String mMark;
    private TextView tvMark, tvName;
    private int mMarkColor;
    public SettingItemView(Context context) {
        super(context);
        initContentView(context);
    }

    public SettingItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParam(context, attrs);
        initContentView(context);
    }

    private void initParam(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
        itemTextSize = typedArray.getFloat(R.styleable.SettingItemView_item_text_size, 14.0f);
        markTextSize = typedArray.getFloat(R.styleable.SettingItemView_mark_text_size, 10.0f);
        mName = typedArray.getString(R.styleable.SettingItemView_item_text_name);
        mMark = typedArray.getString(R.styleable.SettingItemView_item_text_mark);

        mMarkColor = typedArray.getColor(R.styleable.SettingItemView_text_mark_color, context.getResources().getColor(R.color.global_customize_fixed_color_dark_gray));
        typedArray.recycle();
    }

    private void initContentView(Context context) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.settings_item, this, true);
        tvName = convertView.findViewById(R.id.item_resolution);
        icon = convertView.findViewById(R.id.imageButton2);
        tvMark = convertView.findViewById(R.id.settings_item_mark);
        tvName.setText(mName);
        tvName.setTextSize(itemTextSize);
        tvMark.setTextSize(markTextSize);
        if (!StringUtils.isEmpty(mMark)){
            tvMark.setText(mMark);
            tvMark.setVisibility(View.VISIBLE);
        }
        tvMark.setTextColor(mMarkColor);
        setItemViewCheck();
    }

    public void setName(String name){
        tvName.setText(name);
    }

    public void showMark(boolean show){
        tvMark.setVisibility(show? VISIBLE : GONE);
    }

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check){
        this.check = check;
        setItemViewCheck();
    }

    private void setItemViewCheck() {
        if (this.check){
            icon.setVisibility(View.VISIBLE);
            icon.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_yes);
        }else{
            icon.setImageResource(R.drawable.ic_tange_global_icon_radio_btn_no);
            icon.setVisibility(View.VISIBLE);
        }
    }
}
